﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using Zone;
using System.IO;
using System.Net;
using Maticsoft.DBUtility;
using System.Configuration;
using Zone;
using System.Net.Sockets;

namespace SmsSend
{
    public partial class Login : Form
    {
        Func2 fun2 = new Func2();
        public static bool flag = false;

        HttpClient webClient = new HttpClient(); //测试

        Func2 f2 = new Func2();
        public Login()
        {
            InitializeComponent();
            Init_GlobalSetting.wwwAddress = GetWWWAdd(); //取消了app.config文件，更改为从远端config.jsp获得配置信息
        }

        private void Login_Load(object sender, EventArgs e)
        {
            //MessageBox.Show("4");
            loginAd_picbox.ImageLocation = Init_GlobalSetting.wwwAddress + "/pc/ad.png";
            LoadUserAndPwd();                                   
        }
        void NewForm2()
        {
            WebClient_LoadWeb wc = new WebClient_LoadWeb();                        
            string ss = JspGBK(textBox1.Text, textBox2.Text, Init_GlobalSetting.wwwAddress + "login2.jsp");            
            
            //判断登录是否成功
            if (ss.IndexOf("用户名不存在") > 0 || ss.IndexOf("密码不正确") > 0)
            {
                DebugNew.DebugMethod(ss);
                ErrInfo_label.Text = "验证错误！";
            }
            else
            {
                if (ss.IndexOf("Err") >= 0)
                {
                    MessageBox.Show("请检查网络！");
                }
                else
                {
                    MainFm mainFm = new MainFm(webClient);
                    Program.context.MainForm = mainFm;
                    mainFm.Show();
                    this.Close();
                }
            }          
            
        }
        private void Login_KeyDown(object sender, KeyEventArgs e)
        {
            MessageBox.Show("keydown");
        }                             
        /// <summary>
        /// webClient 控件载入页面，输入用户名和密码，返回页面html的字符串
        /// </summary>
        /// <param name="s1"></param>
        /// <param name="s2"></param>
        /// <param name="uriString"></param>
        /// <returns></returns>
        public string JspGBK(string s1, string s2, string uriString)
        {
            string postString = "username=" + s1 + "&password=" + s2 + "&act=1&Submit=";
            string srcString = "";
            webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
            byte[] postData = Encoding.ASCII.GetBytes(postString);
            try
            {
                byte[] responseData = webClient.UploadData(uriString, "POST", postData);
                srcString = Encoding.GetEncoding("GBK").GetString(responseData);
            }
            catch
            {
                srcString = "Error";
            }

            Init_GlobalSetting.CookContainer = webClient.Cookies;
            Init_GlobalSetting.CookCollection = CookieForEach.GetAllCookies(Init_GlobalSetting.CookContainer);
            
            return srcString;

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            ErrInfo_label.Text = "";
            string s = textBox1.Text.Trim();
            if (s.Length > 40)
            {
                textBox1.Text = s.Substring(0, 40);
            }
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            ErrInfo_label.Text = "";
            string s = textBox2.Text.Trim();
            if (s.Length > 40)
            {
                textBox2.Text = s.Substring(0, 40);
            }
        }
        SaveUserPwd saveUserPwd = new SaveUserPwd(); 
        void SaveUserPwds()
        {
                       

            if (checkBox1.Checked)
            {
                saveUserPwd.SaveUserDefault("show", textBox1.Text.Trim(), DESEncrypt.Encrypt(textBox2.Text.Trim(),"putiansms"), "用户");
            }
            else
            {
                saveUserPwd.DeleteUserDefault();
            } 
        }
        void LoadUserAndPwd()
        {
            string Show = saveUserPwd.GetStringRegistryValue("sys_info1", "");
            if (Show == "show") checkBox1.Checked = true;
            textBox1.Text = saveUserPwd.GetStringRegistryValue("sys_info2", "");
            textBox2.Text = DESEncrypt.Decrypt(saveUserPwd.GetStringRegistryValue("sys_info3", ""), "putiansms");
        }
        private void denglu_picbox_Click(object sender, EventArgs e)
        {
            SaveUserPwds();
            NewForm2();
        }

        private void zhuce_picbox_Click(object sender, EventArgs e)
        {
            string RemoteRegUrl = GetRegUrl();
            string str = @"C:\Program Files\Internet Explorer\IEXPLORE.EXE";
            string RegUrl = Init_GlobalSetting.NewReg;
            if (RemoteRegUrl != "") RegUrl = RemoteRegUrl;
            System.Diagnostics.Process.Start(str, RegUrl); 
        }

        private void wangjimima_picbox_Click(object sender, EventArgs e)
        {
            string RemoteGetPwdUrl = GetPwd();
            string str = @"C:\Program Files\Internet Explorer\IEXPLORE.EXE";
            string Pwd = Init_GlobalSetting.GetPwd;
            if (RemoteGetPwdUrl != "") Pwd = RemoteGetPwdUrl;
            System.Diagnostics.Process.Start(str, Pwd); 
        }
        //*********登录地址从远端的config.jsp里读取
        string GetRegUrl()
        {
            string ss2 = (new WebClient_LoadWeb()).JspGBK("http://59.42.242.21:8080/sms_pc/pc/config.jsp", ref webClient);
            string ss3 = fun2.RegexMatch(ss2, "RegUrl.+;"); ss3 = ss3.Replace("RegUrl:", ""); ss3 = ss3.Replace(";", ""); ss3 = ss3.Replace("\r", ""); ss3 = ss3.Replace("\n", "");
            return ss3;
        }
        //*********登录地址从远端的config.jsp里读取
        string GetPwd()
        {
            string ss2 = (new WebClient_LoadWeb()).JspGBK("http://59.42.242.21:8080/sms_pc/pc/config.jsp", ref webClient);
            string ss3 = fun2.RegexMatch(ss2, "GetPwd.+;"); ss3 = ss3.Replace("GetPwd:", ""); ss3 = ss3.Replace(";", ""); ss3 = ss3.Replace("\r", ""); ss3 = ss3.Replace("\n", "");
            return ss3;
        }
        string GetWWWAdd()
        {
            string ss2 = (new WebClient_LoadWeb()).JspGBK("http://59.42.242.21:8080/sms_pc/pc/config.jsp", ref webClient);
            string ss3 = fun2.RegexMatch(ss2, "GetWWWAdd.+;"); ss3 = ss3.Replace("GetWWWAdd:", ""); ss3 = ss3.Replace(";", ""); ss3 = ss3.Replace("\r", ""); ss3 = ss3.Replace("\n", "");
            return ss3;
        }

        private void Login_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                denglu_picbox_Click(null, null);
            }
        }

    }
}
